package com.example.sunmiv1.adapter;

import android.content.Context;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.SkuInfoNew;
import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.ParcelHelper;
import com.example.sunmiv1.views.flowlayout.FlowLayout;
import com.example.sunmiv1.views.flowlayout.TagAdapter;
import com.example.sunmiv1.views.flowlayout.TagFlowLayout;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewAddGoodsSkuAdapter extends RecyclerView.Adapter<NewAddGoodsSkuAdapter.nagsVHolder> {

    private Context context;
    private List<SkuInfoNew> mList;//  數據源,規格集合,需要及時更新
    private List<SkuInfoNew.DataBean> skuDatas;

    //    private List<String> skuClass;// 規格分類名稱  傳入spinner
//    private List<SkuInfoNew.DataBean.ListBean> skuList;  //  全局規格列表

    //    private int classIndex = 0;
    private NewAddGoodsSkuAdapter.Operation operation;
    private ArrayAdapter arrayAdapter;
    private TagAdapter tagAdapter;

    //    private static Spinner spinner;
//    private static TagFlowLayout tagFlowLayout;

    public NewAddGoodsSkuAdapter(Context context, List<SkuInfoNew> list) {
        super();
        this.context = context;
        this.mList = list;
    }

    @Override
    public nagsVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_goods_sku, parent, false);
        return new nagsVHolder(view);
    }

    @Override
    public void onBindViewHolder(nagsVHolder holder, int position) {
        final int index=position;
        holder.setIsRecyclable(false);
        SkuInfoNew skuBean=mList.get(position);
        List<String> skuClass = new ArrayList<>();
        // sku種類數量
        for (SkuInfoNew.DataBean bean : skuBean.getData()) {
            SkuInfoNew.DataBean bean1=ParcelHelper.copy(bean);
            String name=bean1.getName();
            skuClass.add(name);
        }
        for (int i=0;i<mList.size();i++){
            Log.d("規格類型選擇數據",mList.get(i).getData().get(mList.get(i).getSelectFlag()).getName());
            if (i!=position){
                skuClass.remove(mList.get(i).getData().get(mList.get(i).getSelectFlag()).getName());
            }
        }

        final Spinner spinner = holder.skuAdapterSpinner;
        final TagFlowLayout tagFlowLayout = holder.skuAdapterFlow;

        setSpinnerAdapter(spinner,skuClass,true);


            for (int i = 0; i < skuClass.size(); i++) {
                if ((skuBean.getData().get(skuBean.getSelectFlag()).getName().equals(skuClass.get(i)))) {
                    spinner.setSelection(i);
                }
            }

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < skuBean.getData().size(); i++) {
                    if (skuBean.getData().get(i).getName().equals(skuClass.get(position))){
                        if (skuBean.getSelectFlag()!=i){
                            skuBean.getData().get(i).setChecked(true);
                            skuBean.setSelectFlag(i);
                            notifyDataSetChanged();
                        }
                    }else {
                        skuBean.getData().get(i).setChecked(false);
                    }
                }

                LogUtil.i("skuBean.getData().get("+position+").getList().size()"+skuBean.getData().get(position).getList().size());
                setTagAdapter(tagFlowLayout,skuBean.getData().get(skuBean.getSelectFlag()).getList());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        if (skuBean.getData().size()>0) {
            setTagAdapter(tagFlowLayout, skuBean.getData().get(skuBean.getSelectFlag()).getList());
        }

        tagFlowLayout.setOnTagClickListener((view, positions, parent1) -> {
            operation.setChoose(position,positions);
            skuBean.getData().get(skuBean.getSelectFlag()).getList().get(positions).setChecked(
                    !skuBean.getData().get(skuBean.getSelectFlag()).getList().get(positions).isChecked()
            );
            return true;
        });

        final View delete=holder.skuAdapterDelete;
        delete.setOnClickListener(v -> operation.delete(index));

        // 添加一個規格類型
        final View addClass=holder.skuAdapterClassAdd;
        final EditText editClass=holder.skuAdapterClassEdit;
        addClass.setOnClickListener(v -> {
            String classStr=editClass.getText().toString().trim();
            if (!TextUtils.isEmpty(classStr)) {
                boolean flag=false;
                for (SkuInfoNew.DataBean bean : skuBean.getData()) {
                    if (classStr.equals(bean.getName())) {
                        flag=true;
                        editClass.setText("");
                        arrayAdapter.notifyDataSetChanged();
                        spinner.setSelection(skuBean.getData().indexOf(bean));
                        break;
                    }
                }
                if(!flag){
                    arrayAdapter.notifyDataSetChanged();
                    operation.addClass(position, classStr);
                }
            }
        });

        // 添加規格
        final View addSku=holder.skuAdapterSkuAdd;
        final EditText editSku=holder.skuAdapterSkuEdit;
        addSku.setOnClickListener(v -> {
            String skuStr=editSku.getText().toString().trim();
            if (!TextUtils.isEmpty(skuStr)) {
                boolean flag=false;
                for (SkuInfoNew.DataBean.ListBean bean : skuBean.getData().get(skuBean.getSelectFlag()).getList()) {
                    if (skuStr.equals(bean.getName())) {
                        flag=true;
                        bean.setChecked(true);
                        editSku.setText("");
                        setTagAdapter(tagFlowLayout,skuBean.getData().get(skuBean.getSelectFlag()).getList());
                        break;
                    }
                }
                if (!flag) {
                    operation.addSku(skuBean.getSelectFlag(), skuBean.getData().get(skuBean.getSelectFlag()).getId(), skuStr);
                    setTagAdapter(tagFlowLayout,skuBean.getData().get(skuBean.getSelectFlag()).getList());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public interface Operation {
        void delete(int position);

        void addClass(int position, String className);

        /**
         *
         * @param classIndex        spinner選擇的當前分類下標
         * @param classId           分類ID
         * @param skuName           規格name
         */
        void addSku(int classIndex, String classId, String skuName);
        void setChoose(int index,int skuIndex);

        void refreshHeight();
    }

    // sipnner適配器
    private void setSpinnerAdapter(Spinner spinner,List<String> skuClass,boolean isNew) {
        if(arrayAdapter!=null&&!isNew){
            arrayAdapter.notifyDataSetChanged();
        }else{
            arrayAdapter = new ArrayAdapter(context, R.layout.item_select, skuClass);
            arrayAdapter.setDropDownViewResource(R.layout.item_drop);
            spinner.setAdapter(arrayAdapter);
        }
    }

    // tagFlow適配器
    private void setTagAdapter(TagFlowLayout tagFlowLayout,List<SkuInfoNew.DataBean.ListBean> skuList) {
//        if(tagAdapter!=null){
//            LogUtil.i("tagAdapter.notifyDataChanged()觸發一次");
//            tagAdapter.notifyDataChanged();
//            return;
//        }
         tagAdapter = new TagAdapter(skuList) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {
                TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.tv_shop, tagFlowLayout, false);
                tv.setText(((SkuInfoNew.DataBean.ListBean) o).getName());
                if (skuList.get(position).isChecked()) {
                    tv.setTextColor(context.getResources().getColor(R.color.login_textColor1));
                }
                return tv;
            }
        };
        tagFlowLayout.setAdapter(tagAdapter);
    }

    public void Click(NewAddGoodsSkuAdapter.Operation operation) {
        this.operation = operation;
    }

    static class nagsVHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.skuAdapter_spinner)
        Spinner skuAdapterSpinner;
        @BindView(R.id.skuAdapter_delete)
        TextView skuAdapterDelete;
        @BindView(R.id.skuAdapter_classEdit)
        EditText skuAdapterClassEdit;
        @BindView(R.id.skuAdapter_classAdd)
        TextView skuAdapterClassAdd;
        @BindView(R.id.ll_add_class)
        LinearLayout llAddClass;
        @BindView(R.id.skuAdapter_skuEdit)
        EditText skuAdapterSkuEdit;
        @BindView(R.id.skuAdapter_skuAdd)
        TextView skuAdapterSkuAdd;
        @BindView(R.id.ll_add_sku)
        LinearLayout llAddSku;
        @BindView(R.id.skuAdapter_flow)
        TagFlowLayout skuAdapterFlow;
        public nagsVHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
